package com.bjpowernode.javaweb.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class RequestTextServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println(request); //org.apache.catalina.connector.RequestFacade@27554e8f

        //获取客户端IP地址
        String addr = request.getRemoteAddr();
        out.println(addr); //127.0.0.1

        //动态获取应用的根路径
        String path=request.getContextPath();
        System.out.println(path);

        //获取请求方式
        System.out.println(request.getMethod());

        System.out.println(request.getRequestURI());

        System.out.println(request.getServletPath());

    }

//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        Set<String> strings = parameterMap.keySet();
//        Iterator<String> iterator = strings.iterator();
//        while (iterator.hasNext()){
//            String key=iterator.next();
//            String[] strings1 = parameterMap.get(key);
//            for(String value:strings1){
//                System.out.print(key+"="+value+" ");
//            }
//            System.out.println();
//        }
//
//        //获取所有的key
//        Enumeration<String> parameterNames = request.getParameterNames();
//        while (parameterNames.hasMoreElements()){
//            String key=parameterNames.nextElement();
//            System.out.println(key);
//        }
//
//        //直接通过name获取一维数组
//        String[] usernames = request.getParameterValues("username");
//        String[] userpwds = request.getParameterValues("userpwd");
//        String[] aihaos = request.getParameterValues("aihao");
//        for(String username:usernames){
//            System.out.println(username);
//        }
//
//        for(String password:userpwds){
//            System.out.println(password);
//        }
//
//        for(String aihao:aihaos){
//            System.out.println(aihao);
//        }
//
//        //通过name获取一维数组中的第一个元素
//        String username=request.getParameter("username");
//        String userpwd=request.getParameter("userpwd");
//        String aihao=request.getParameter("aihao");
//        System.out.println(username+","+userpwd+","+aihao);
//
//    }
}
